<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* 标签模型
*
*/

class Tag_model extends MY_Model {

	// 记录本次操作中的有效标签值
	var $curtags = array();
	var $_pre;

	function __construct()
	{
		if (!empty($this->_d['lg'])) $this->_pre = "_" . $this->_d['lg'];
		parent::__construct();
		$this->tbl = 'ey_tag' . $this->_pre;
		$this->tbl_key = 'tagid';
		$this->tbl_rel = 'ey_tag_relation' . $this->_pre;
	}

	/**
	* 增加批量标签
	* @param $tagname 标签名称数组
	* @param $tagname 原标签名称数组
	* @param $relationid 标签相关ID
	* @param $channel 所属频道名称 
	*/
	public function setTags($tagname, $oldtagname,$relationid='', $channel='news')
	{
		if (empty($tagname)) 
		{
			$this->delTag($relationid, $channel);
		}
		else if (is_array($tagname))
		{
			foreach ($tagname as $k => $v)
			{
				$newisold = 0;
				$oldtagid = '';
				if (!empty($oldtagname))
				{
					foreach ($oldtagname as $k2 => $v2)
					{
						if ($v == $v2['tagname'])
						{
							$newisold = 1;
							unset($oldtagname[$k2]);
						}
					}
				}

				if ($newisold == 0)
				{
					if ($v) $this->setTag($v, $relationid, $channel);
				}				
			}
			
			if (!empty($oldtagname))
			{
				foreach ($oldtagname as $k => $v) $oldtagid[] = $v['tagid'];
				$this->delTag($relationid, $channel,implode(",",$oldtagid));
			}		
			
			if (!empty($relationid)) $this->setRelation($relationid, $channel);
		}
	}

	/**
	* 增加单标签
	* @param $tagname 标签名称
	* @param $relationid 标签相关ID
	* @param $channel 所属频道名称 
	*/
	public function setTag($tagname, $relationid='', $channel='news')
	{
		if (empty($tagname)) return false;

		$tag = $this->hasTag($tagname);
		
		if ($tag)
		{
			empty($relationid) ? $tagnum =0 : $tagnum = 1;
			$this->M(array('tagnum'=>$tag['tagnum'] + $tagnum),array('tagid'=>$tag['tagid']));
		}
		else
		{
			empty($relationid) ? $tagnum =0 : $tagnum = 1;
			$tag = $this->addTag($tagname, $tagnum);
		}

		array_push($this->curtags, $tag);
	}


	/**
	* 设置标签关联关系
	*/
	private function setRelation($relationid, $channel='news')
	{
		
		if (empty($relationid)) return false;
		//$sql = "delete from " . $this->tbl_rel . " where channel='{$channel}' and relationid='{$relationid}'";
		//$this->db->query($sql);
		if (!empty($this->curtags))
		{
			$postdata = array();
			$ctime = time();
			$i = 0;
			foreach ($this->curtags as $k =>$v )
			{
				$postdata[$i]['tagid'] = $v['tagid'];
				$postdata[$i]['relationid'] = $relationid;
				$postdata[$i]['channel'] = $channel;
				$postdata[$i]['ctime'] = $ctime;
				$i++;
			}

			$this->db->insert_batch($this->tbl_rel, $postdata);
		}
	}

	/**
	* 刷新tag标签统计项
	*/
	public function resetTagNum()
	{
		$sql = "select tagid,count(tagid) as tagnum, UNIX_TIMESTAMP() as mtime  from " . $this->tbl_rel . " group by tagid";
		$query = $this->db->query($sql);
		$resetdata = $query->result_array();
		if (!empty($resetdata))
			$this->db->update_batch($this->tbl, $resetdata, 'tagid');
	}

	/**
	* 增加多标签
	*/
	private function addTag($tagname, $tagnum = 0)
	{
		$postdata['tagname'] = $tagname;
		$postdata['tagnum'] = $tagnum;
		$postdata['ctime'] = time();
		$postdata['tagid'] = $this->A($postdata);
		return $postdata;
	}

	/**
	* 判断是否已存在该标签
	*/
	function hasTag($tagname)
	{
		return $this->O(array('tagname'=>$tagname));
	}

	function getTags($tagchannel = 'news', $limit='20')
	{
		if ($limit != '') $limit = " limit " . $limit;
		$sql = "select a.tagid,a.tagname,a.tagnum from " . $this->tbl . " as a 
				 order by a.tagnum desc" . $limit;
		$query = $this->db->query($sql);
		return  $query->result_array();
	}

	/**
	* 删除文章引起的标签删除
	*/
	function delTag($relationid,$channel,$tagid='')
	{
		if (is_array($relationid)) $relationid = implode(",",$relationid);
		$sql = "select * from " . $this->tbl_rel . " where relationid in ( ".$relationid. ") and channel = '" . $channel."'";
		if ($tagid != '') $sql = $sql . " and tagid in ( " . $tagid . ")";
		$query = $this->db->query($sql);
		$relationtag = $query->result_array();
		if (!empty($relationtag))
		{
			foreach ($relationtag as $k => $v)
			{
				$this->db->query("update ".$this->tbl." set tagnum = tagnum -1 where tagid = ".$v['tagid']);
			}
		}

		$sql = "delete from ".$this->tbl." where tagnum = 0";
		$this->db->query($sql);

		$sql = "delete from ".$this->tbl_rel." where relationid in ( ".$relationid. ") and channel = '" . $channel."'";
		if ($tagid != '') $sql = $sql . " and tagid in ( " . $tagid . ")";
		$this->db->query($sql);
	}

}
?>